/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.topic.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.content.ContentDao;
import org.grayrabbit.cms.dao.topic.TopicDao;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.service.topic.TopicService;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.grayrabbit.cms.vo.topic.TopicBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 15:59:25  
 */
@Service
@Transactional
public class TopicServiceImpl extends GenericServiceImpl implements TopicService {
	private  static final Logger  logger = LoggerFactory.getLogger(TopicServiceImpl.class);
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("topicDaoImpl")
	private TopicDao topicDao;
	
	@Autowired
	@Qualifier("contentDaoImpl")
	private ContentDao contentDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see TopicService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public Topic getTopic(final String topicId){
		Topic topic = topicDao.findById(topicId);
		topicDao.initialize(topic);
		topicDao.initialize(topic.getSite());
		return topic;
	}
	
	/**
	 * @see TopicService#delete(String)
	 */
	@Transactional
	public void delete(final Topic topic) {
		topicDao.makeTransient(topic);
	}
	
	/**
	 * @see TopicService#save(OperLog)
	 */
	@Transactional
	public void save(final Topic topic) {
		topicDao.makePersistent(topic);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see TopicService#getPage(TopicBean)
	 */
	@Transactional(readOnly=true)
	public Page<Topic> getPage(final TopicBean topicBean){
		Page<Topic> page =  new Page<Topic>();
		page.setTotal(topicDao.count(topicBean));
		page.setResult(topicDao.findByQc(topicDao.assembly(topicBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see TopicService#list(Page, TopicBean)
	 */
	public void list(Page<Topic> page, TopicBean topicBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(topicDao.count(topicBean));
		}
		page.setResult(topicDao.findByPage(page,topicBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see TopicService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			topicDao.makeTransient(topicDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				topicDao.clear();
		}
		
	}
	/**
	 * 根据站点ID查询专题列表
	 */
	public List<Topic> findBySiteId(String siteId) {
		
		return topicDao.findBySiteId(siteId);
	}
 
	/**
	 * 批量启用专题
	 * @param keys
	 */
	public void enable(String[] keys){
		if(keys!=null){
			for(String str: keys){
				Topic a=topicDao.findById(str);
				a.setIsEnable(true);
				topicDao.makePersistent(a);
			}
		}
		
	}
	/**
	 * 批量禁用专题
	 * @param keys
	 */
	public void disable(String[] keys){
		if(keys!=null){
			for(String str:keys){
				Topic a=topicDao.findById(str);
				a.setIsEnable(false);
				topicDao.makePersistent(a);
			}
		}
	}
	/**
	 * 批量审核专题
	 */
	public void audit(String[] keys){
		if(keys!=null){
			for(String str:keys){
				Topic a=topicDao.findById(str);
				a.setIsAudit(true);
				topicDao.makePersistent(a);
			}
		}
	}
	/**
	 * 批量禁止审核专题
	 */
	public void noAudit(String[] keys){
		if(keys!=null){
			for(String str:keys){
				Topic a=topicDao.findById(str);
				a.setIsAudit(false);
				topicDao.makePersistent(a);
			}
		}
	}
	
	/**
	 * 检查同一站点下专题名称唯一性
	 * @param role
	 * @return
	 */
	public Boolean checkNameUnique(String topicName,String tname,String siteId){
		return topicDao.checkNameUnique(topicName,tname,siteId);
	}
	
	
	public List<Topic> ftlFindTopics(String[] siteIds,String[] topicIds,long[] limit){
		try {
			return topicDao.ftlFindTopics(siteIds,topicIds,limit);
		} catch (Exception e) {
			logger.error("专题查询异常", e);
		}
		return new ArrayList<Topic>();
	}
	
	/**
	 * 根据站点获取专题
	 * @param list
	 * @param currentTopicListIndex
	 * @param max
	 * @return
	 */
	public List<String> findTopicIdsBySiteId(List<String> list,
			int currentTopicListIndex, int max){
		List<String> rs = new ArrayList<String>();
		for(String siteId:list){
			List<Topic> topicList = topicDao.findPublishBySiteId(siteId, currentTopicListIndex, max);
			for(Topic topic:topicList){
				rs.add(topic.getTopicId());
			}
		}
		return rs;
	}
	/**
	 * 获取专题下文章数
	 * @param list
	 * @return
	 */
	public int countContentByTopics(List<String> list){
		int count = 0;
		for(String topicId : list){
			ContentBean contentBean = new ContentBean();
			Topic topic = new Topic();
			topic.setTopicId(topicId);
			contentBean.setTopic(topic);
			count+=contentDao.countAllByPublish(contentBean);
		}
		return count;
	}
	/**
	 * 获取专题下增量文章数
	 * @param list
	 * @return
	 */
	public int countMoreContentByTopics(List<String> list){
		int count = 0;
		for(String topicId : list){
			Topic topic = topicDao.findById(topicId);
			ContentBean contentBean = new ContentBean();
			contentBean.setTopic(topic);
			if(topic.getIsAudit()!=null && topic.getIsAudit().booleanValue()){
				contentBean.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_SUCCESS);
			}else{
				contentBean.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
			}
			contentBean.setMaxPublishTime(new Date());
			count+=contentDao.count(contentBean);
		}
		return count;
	}
	/**
	 * 获取专题下文章
	 * @param topicId
	 * @return
	 */
	public List<Content> findByTopId(String topicId) { 
		List<Content>contents=contentDao.findByTopicId(topicId);
		return contents;
	}
	/*
	 * 专题简称唯一验证
	 */
	public Boolean checkSimplenameunique(String simName, String siteId,
			String oldSimpleName) {
		return topicDao.checkSimplenameunique(simName, siteId, oldSimpleName);
		
	}
	
}